package org.jweb.demo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.jweb.core.bean.PageMode;
import org.jweb.core.dao.IHibernateCommonDao;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.jweb.core.service.impl.CommonServiceImpl;
import org.jweb.core.util.MyBeanUtils;
import org.jweb.demo.entity.DemoGoodsEntity;
import org.jweb.demo.entity.DemoGoodsPicEntity;
import org.jweb.demo.entity.DemoGoodsTypeDicEntity;
import org.jweb.demo.entity.DemoGoodsTypeREntity;
import org.jweb.demo.service.DemoGoodsServiceI;
import org.jweb.demo.trans.form.DemoGoodsSaveForm;
import org.jweb.demo.trans.form.DemoGoodsSearchForm;
import org.jweb.demo.trans.vo.DemoGoodsPicVO;
import org.jweb.demo.trans.vo.DemoGoodsTypeDicVO;
import org.jweb.demo.trans.vo.DemoGoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("demoGoodsService")
@Transactional
public class DemoGoodsServiceImpl extends CommonServiceImpl implements DemoGoodsServiceI {

	@Autowired
	private IHibernateCommonDao hibernateCommonDao;//该dao提供一些不依赖于具体前端js框架的查询

	@Override
	public PageMode<DemoGoodsVO> list(DemoGoodsSearchForm searchForm,int pageNo,int pageSize) {
		PageMode<DemoGoodsVO> resultPage  = new PageMode<DemoGoodsVO>(); 
		
		searchForm.emptyValue2NullValue();
		CriteriaQuery cq = searchForm.getCriteriaQuery();
		PageMode<DemoGoodsEntity> goodsPage = this.hibernateCommonDao.list4page(cq,pageNo,pageSize);
		
		List<DemoGoodsEntity> goodsList = goodsPage.getResult();
		List<DemoGoodsVO> goodsVOList = new ArrayList<DemoGoodsVO>();
		for(DemoGoodsEntity g : goodsList){
			DemoGoodsVO goodsVO = new DemoGoodsVO();
			goodsVO.copyEntity(g);
			
			//手工转换goods类型
			List<DemoGoodsTypeDicVO> dicList = new ArrayList<DemoGoodsTypeDicVO>();
			Set<DemoGoodsTypeREntity> rSet = g.getTypeSet();
			for(DemoGoodsTypeREntity r : rSet){
				DemoGoodsTypeDicEntity dicEntity = r.getTypeEntity();
				DemoGoodsTypeDicVO dicVO = new DemoGoodsTypeDicVO();
				dicVO.setCode(dicEntity.getCode());
				dicVO.setId(dicEntity.getId());
				dicVO.setName(dicEntity.getName());;
				dicVO.setParent(dicEntity.getParent());
				
				
//				dicVO.copyEntity(dicEntity);
				dicList.add(dicVO);
			}
			goodsVO.setTypeList(dicList);
			
			//手工转换goods详细图片
			List<DemoGoodsPicVO> picList = new ArrayList<DemoGoodsPicVO>();
			Set<DemoGoodsPicEntity> picSet = g.getPicSet();
			for(DemoGoodsPicEntity p : picSet){
				DemoGoodsPicVO picVO = new DemoGoodsPicVO();
				
				picVO.copyEntity(p);
				picList.add(picVO);
			}
			goodsVO.setPicList(picList);
			
			//最后将goodsVO放到List集合中
			goodsVOList.add(goodsVO);
		}
		
		
		//复制实体page到vo page中
		BeanUtils.copyProperties(goodsPage, resultPage, new String[]{"result"});
		resultPage.setResult(goodsVOList);
		return resultPage;
	}

	@Override
	public DemoGoodsVO get(String goodsId) {
		DemoGoodsEntity entity = (DemoGoodsEntity)this.hibernateCommonDao.get(DemoGoodsEntity.class, goodsId);
		DemoGoodsVO vo = new DemoGoodsVO();
		vo.copyEntity(entity);
		
		//手工封装类型信息
		vo.setTypeList(new ArrayList<DemoGoodsTypeDicVO>());
		Set<DemoGoodsTypeREntity> rSet = entity.getTypeSet();
		for(DemoGoodsTypeREntity r : rSet){
			DemoGoodsTypeDicEntity dicEntity = r.getTypeEntity();
			DemoGoodsTypeDicVO dicVO = new DemoGoodsTypeDicVO();
			dicVO.setCode(dicEntity.getCode());
			dicVO.setId(dicEntity.getId());
			dicVO.setName(dicEntity.getName());
			dicVO.setParent(dicEntity.getParent());
//			dicVO.copyEntity(dicEntity);
			vo.getTypeList().add(dicVO);
		}
		//手工封装图片信息
		vo.setPicList(new ArrayList<DemoGoodsPicVO>());
		Set<DemoGoodsPicEntity> pSet = entity.getPicSet();
		for(DemoGoodsPicEntity p : pSet){
			DemoGoodsPicVO picVO = new DemoGoodsPicVO();
			picVO.copyEntity(p);
			vo.getPicList().add(picVO);
		}
		
		
		
		return vo;
	}

	@Override
	public void add(DemoGoodsSaveForm saveForm) {
		DemoGoodsEntity entity = (DemoGoodsEntity)saveForm.toEntity();
		this.hibernateCommonDao.save(entity);
		
	}
	@Override
	public void update(DemoGoodsSaveForm saveForm) {
		DemoGoodsEntity newEntity = (DemoGoodsEntity)saveForm.toEntity();
		DemoGoodsEntity oldEntity = (DemoGoodsEntity)this.hibernateCommonDao.get(DemoGoodsEntity.class, saveForm.getId());
		
		//清空集合再保存
		this.hibernateCommonDao.batchDelete(oldEntity.getPicSet());
//		Set<DemoGoodsPicEntity> oldPicSet = oldEntity.getPicSet();
//		for(DemoGoodsPicEntity p : oldPicSet){
//			this.hibernateCommonDao.delete(p);
//		}
		
		//
		this.hibernateCommonDao.batchDelete(oldEntity.getTypeSet());
//		Set<DemoGoodsTypeREntity> oldRSet = oldEntity.getTypeSet();
//		for(DemoGoodsTypeREntity r : oldRSet){
//			this.hibernateCommonDao.delete(r);
//		}
		
		
		try {
			MyBeanUtils.copyBeanNotNull2Bean(newEntity, oldEntity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.hibernateCommonDao.update(oldEntity);
		
	}

	@Override
	public void add(DemoGoodsEntity entity) {
		this.hibernateCommonDao.save(entity);
		
	}

	@Override
	public void update(DemoGoodsEntity entity) {
		
		this.hibernateCommonDao.update(entity);
		
	}

	
}
